import { ref, computed } from 'vue'
import { defineStore } from 'pinia'
import axios from 'axios'

export const useCounterStore = defineStore('counter', () => {
  // 定义数据 (count)
  const count = ref(0)

  // getter 定义, 可以理解为监听属性, 使用方可以直接来展示
  const doubleCount = computed(() => count.value * 2)

  // 定义修改数据的方法 (action 同步 + 异步)
  function increment() {
    count.value++
  }

  // 定义异步 action
  const list = ref([])
  const getList = async () => {
    const res = await axios.get("/user")

  }

  return { count, doubleCount, increment, getList }
})

// 对象方式的写法
defineStore('counter', {
  state: () => ({count: 10}),
  getters: {
    double: (state) => state.count * 2,
  },
  actions: {
    increment() {
      this.count ++
    }
  }
})